package com.shioirikukuri.mapper;

import com.shioirikukuri.dto.ComboItemDTO;
import com.shioirikukuri.entity.Combo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ComboMapper {

	@Select("SELECT * FROM combos WHERE id = #{id}")
	Combo findById(Long id);


	@Insert("INSERT INTO combos(name, price, image_url, available, created_at, updated_at) " +
			"VALUES(#{name}, #{price}, #{imageUrl}, #{available}, NOW(), NOW())")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	Integer insert(Combo combo);

	@Delete("DELETE FROM combos WHERE id = #{id}")
	void deleteById(Long id);

	@Delete("DELETE FROM combo_items WHERE food_id = #{foodId} AND combo_id = #{comboId}")
	void deleteComboItems(@Param("comboId") Long comboId);


	@Update("UPDATE combos SET name=#{name}, price=#{price}, image_url=#{imageUrl}, " +
			"available=#{available}, updated_at=NOW() WHERE id=#{id}")
	void update(Combo combo);

	@Delete("DELETE FROM combo_items WHERE combo_id = #{comboId}")
	void deleteComboItemsByComboId(@Param("comboId") Long comboId);

	@Insert({
			"<script>",
			"INSERT INTO combo_items(combo_id, food_id, quantity)",
			"VALUES ",
			"<foreach collection='items' item='item' separator=','>",
			"(#{comboId}, #{item.foodId}, #{item.quantity})",
			"</foreach>",
			"</script>"
	})
	int batchInsertComboItems(@Param("comboId") Long comboId, @Param("items") List<ComboItemDTO> items);


	@Select("SELECT * FROM combos")
	List<Combo> findAll();

	@Select("SELECT combo_id, food_id, quantity FROM combo_items WHERE combo_id = #{comboId}")
	List<Combo.ComboItem> findComboItemsByComboId(@Param("comboId") Long comboId);


}